/*
 * Copyright (c) 2011 Team A CS2340. All rights reserved.
 */

package edu.gatech.teama.doctorsoffice.model;

import java.io.IOException;
import java.io.Serializable;

import java.util.Collection;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.joda.time.LocalDate;
import org.joda.time.Period;

/**
 * The Class Patient creates a patient user.
 * 
 * @author TeamA
 * @version 1.0
 */
public class Patient extends User implements Serializable{
    //private PatientHealthHistory healthReport;
	/** The pharmacy. */
    private String pharmacy;
	
	/** The gender. */
	private Gender gender;
	
	/** The date of birth. */
	private LocalDate dateOfBirth;
	
	/** The insurance carrier. */
	private String insuranceCarrier;
	
	/** The allergies. */
	private static final Collection<String> ALLERGIES=null;
	 // $codepro.audit.disable blankLineUsage
	///** The medical history. */
	//private Collection<TreatmentRecord> medicalHistory=null;
	
    /** The my logger. */
    private static final Logger MYLOGGER = 
    	Logger.getLogger("edu.gatech.teama.doctorsoffice.model.Patient");
    
    static {
        FileHandler fh = null;
        try {
            fh = new FileHandler("application.log");
        } catch (SecurityException e) {
            MYLOGGER.log(Level.SEVERE, 
            		"Security Exception creating the logger file handler");
            e.printStackTrace();
        } catch (IOException e) {
            MYLOGGER.log(Level.SEVERE, 
            		"IO Exception creating the logger file handler");
            e.printStackTrace();
        }

        MYLOGGER.addHandler(fh);
        MYLOGGER.setLevel(Level.ALL);
    }

    /**
     * Instantiates a new patient.
     *
     * @param username the username
     */
    public Patient(String username){
    	super(username);
        MYLOGGER.log(Level.INFO, "Patient: " + username);
    }
    
    /**
     * Instantiates a new patient.
     *
     * @param username the username
     * @param firstName the first name
     * @param lastName the last name
     * @param phoneNumber the phone number
     * @param address the address
     * @param emailAddress the email address
     * @param gender the gender
     * @param dob the dob
     * @param pharmacy the pharmacy
     * @param insuranceCarrier the insurance carrier
     */
    public Patient(String username, String firstName, String lastName, 		// $codepro.audit.disable largeNumberOfParameters
    		String phoneNumber, Address address, String emailAddress, 
    		Gender gender, LocalDate dob, String pharmacy, String insuranceCarrier){
        super(username, firstName, lastName, phoneNumber, address, emailAddress);
        this.gender = gender;
        this.dateOfBirth = dob;
        this.pharmacy = pharmacy;
        this.insuranceCarrier = insuranceCarrier;
        MYLOGGER.log(Level.INFO, "New Patient: " + username + ", " + firstName
        		+ " " + lastName + ", " + phoneNumber + ", "
        		+ address + ", " + emailAddress);     
    }
    
    /**
     * Adds the allergy.
     *
     * @param allergy the allergy
     */
    public void addAllergy(String allergy) {
    	if (!ALLERGIES.contains(allergy)){
    		ALLERGIES.add(allergy);
    	}
    }
    
    /**
     * Checks if is allergic to.
     *
     * @param a the a
     * @return true, if is allergic to
     */
    public boolean isAllergicTo(String a) {
    	return ALLERGIES.contains(a);
    }

	/**
	 * Sets the pharmacy.
	 *
	 * @param pharmacy the new pharmacy
	 */
	public void setPharmacy(String pharmacy) {
		this.pharmacy = pharmacy;
	}

	/**
	 * Gets the pharmacy.
	 *
	 * @return the pharmacy
	 */
	public String getPharmacy() {
		return pharmacy;
	}

	/**
	 * Sets the insurance carrier.
	 *
	 * @param insuranceCarrier the new insurance carrier
	 */
	public void setInsuranceCarrier(String insuranceCarrier) {
		this.insuranceCarrier = insuranceCarrier;
	}

	/**
	 * Gets the insurance carrier.
	 *
	 * @return the insurance carrier
	 */
	public String getInsuranceCarrier() {
		return insuranceCarrier;
	}

	/**
	 * Sets the gender.
	 *
	 * @param gender the new gender
	 */
	public void setGender(Gender gender) {
		this.gender = gender;
	}

	/**
	 * Gets the gender.
	 *
	 * @return the gender
	 */
	public Gender getGender() {
		return gender;
	}
	
	/**
	 * Gets the age.
	 *
	 * @return the age
	 */
	public int getAge() {
		// TODO: Take months into account.
		final LocalDate currentDate = new LocalDate();
		final Period timePeriod = new Period(dateOfBirth, currentDate);
		return timePeriod.getYears();
		//return Calendar.getInstance().get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR);
	}
	
	/**
     * toString() method returns the patient's first and last name
     * 
     * @return The patient's first and last name
     */
	@Override
	public String toString() {
		return "Patient: " + getFirstName() + " "
				+ getLastName();
	}
    
    
}



